package pingce.share.util;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pingce.share.model.PairValue;

/**
 * Add preprocess steps to real splitter, including: keep hash tag, keep url, 
 * keep "@user"(deal with two types: only "@user", or "|| @user: " ), keep emoticons(/xxx)
 * 
 * 将句子分成若干子句，便于后续的分词和句法分析。子句的类型包括：
 * 正常句子 NORMAL
 * 话题 HASHTAG(#\S+#)
 * URL URL
 * 用户（形如 @user）USER 
 * 转发标志（"|| @user: "）RETWEET
 * 表情符号 EMOTICON
 * @author ucai
 */
public class WrappedSplitter {

	private Splitter otherSplitter;
//	private static final String regex = "#\\S+?#|http://url.cn/\\w{6}|@\\S+|\\|\\|\\s@\\S+:\\s|/\\S+";
	private static final String regex = "#\\S+?#|http://url.cn/\\w{6}|@\\d+|\\|\\|\\s@\\S+:\\s";
	private static Pattern p = Pattern.compile(regex);
	private static EmoticonProcessor ep;
	
	public static final int NORMAL = 0;
	public static final int HASHTAG = 1;
	public static final int URL = 2;
	public static final int USER = 3;
	public static final int REWTEET = 4;
	public static final int EMOTICON = 5;
	
	public WrappedSplitter(Splitter otherSplitter) {
		super();
		this.otherSplitter = otherSplitter;
		try{
			this.ep = new EmoticonProcessor();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private int getType(String text){
		if(text.startsWith("#"))
			return this.HASHTAG;
		if(text.startsWith("http"))
			return this.URL;
		if(text.startsWith("@"))
			return this.USER;
		if(text.startsWith("|"))
			return this.REWTEET;
		if(text.startsWith("/"))
			return this.EMOTICON;
		return -1;
	}
	
	private List<PairValue> cutToClause(String rawStr){
		List<PairValue> list = new LinkedList<PairValue>();
		Matcher matcher = p.matcher(rawStr);
		int offset = 0;
		while(matcher.find()){
			int start = matcher.start();
			int end = matcher.end();
			String text = matcher.group();
			if(offset < start){
				String subText = rawStr.substring(offset, start);
				PairValue pv = new PairValue(subText, new Integer(this.NORMAL).toString());
				list.add(pv);
			}
			int type = getType(text);
			PairValue pv = new PairValue(text, new Integer(type).toString());
			list.add(pv);
			offset = end;
		}
		int len = rawStr.length();
		if(offset < len){
			String subText = rawStr.substring(offset);
			PairValue pv = new PairValue(subText, new Integer(this.NORMAL).toString());
			list.add(pv);
		}
		return ep.getEmoticons(list);
	}
	

	public List<PairValue> Split(String rawStr, boolean withPOS) {
		List<PairValue> list = this.cutToClause(rawStr);
		return list;
	}

	public Splitter getOtherSplitter() {
		return otherSplitter;
	}

	public void setOtherSplitter(Splitter otherSplitter) {
		this.otherSplitter = otherSplitter;
	}
	
	public static void main(String[] args)throws Exception{
		WrappedSplitter ws = new WrappedSplitter(SplitterFactory.getSplitter("ICT"));
		String str = "#皮鞋果冻# /吐在中国老百姓还敢吃什么？";
		List<PairValue> list = ws.Split(str, true);
		System.out.println("start to print:" + list.size());
		for(PairValue p : list){
			System.out.println(p.getBasic() + "\t" + p.getAttach());
		}
	}
}
